/**
 * SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
 * SPDX-License-Identifier: Mulan PSL v2
 */
<!--
 * @Author: zhb
 * @Date: 2020-08-28
 * @Description: es数据源表单配置
 -->
<template>
    <div class="select-file__table">
        <sf-button
            v-if="returnList.length > 1"
            ref="submit"
            class="creat-button btn-blank return"
            icon-cls="iconchexiao"
            @click="_returnBack">
            <lang>返回上一级</lang>
        </sf-button>
        <sf-table
            ref="table"
            :options="options_fix">
            <sf-table-column 
                data-index="name"
                :title="_('文件名称')"
                :sort-able="true"
                :default-width="180">
                <lang>文件名称</lang>
            </sf-table-column>
            <sf-table-column 
                data-index="size"
                :sort-able="true"
                :default-width="80">
                <lang>大小（MB）</lang>
            </sf-table-column>
            <sf-table-column 
                data-index="lastModified"
                :sort-able="true"
                :default-width="100">
                <lang>最近修改时间</lang>
            </sf-table-column>
            <template slot="name"
                      slot-scope="{record}">
                <span v-if="!isFileFolder(record)">{{ record.name }}</span>
                <span v-else
                      class="pointer"
                      @click="_fileNext(record)">{{ record.name }}</span>
            </template> 
            <template slot="size"
                      slot-scope="{record}">
                <span v-if="isFileFolder(record)">-</span>     
                <span v-else>{{ MbTranslate(record.size) }}</span>
            </template>
            <template slot="lastModified"
                      slot-scope="{record}">
                <span v-if="isFileFolder(record)">-</span>     
                <span v-else>{{ encodeTime(record.lastModified) }}</span>
            </template>
        </sf-table>
    </div>
</template>

<script>
import axios from 'axios';
import { encodeDate } from 'src/util/format';
import { deepClone } from 'src/util/deep_clone';

import { getStorage } from 'src/util/local_storage';

const BYTE = 1024;

export default {
    name: 'SelectFileTable',
    props: {
    },
    data () {
        return {
            options_fix: {
                bufferView: false,

                // 配置这个后才可以实现保持勾选状态
                idProperty: 'id',
                selectionHide: false,
                forceFit: false,
                selectionType:'radio'
            },
            webrpcParams:{
                id: 1,
                jsonrpc: '2.0',
                method: 'web.ListObjects',
                params:{
                    bucketName: '',
                    prefix: ''
                }
            },
            returnPerfix:'',
            returnList:['']
        };
    },
    computed: {
        showReturnText () {
            return !!this.prefix;
        },
        prefix () {
            return this.webrpcParams.params.prefix;
        }
        
    },
    mounted () {
        
    },
    methods: {
        isFileFolder (record) {
            return record.size === 0; //如果size为0 ，则为文件夹。
        },
        _returnBack () {
            this.returnList.pop();
            this.webrpcParams.params.prefix = this.returnList[this.returnList.length - 1];
            this._getWebprc();
        },
        _fileNext (record) {
            this.returnPerfix = deepClone(this.webrpcParams.params.prefix);
            this.webrpcParams.params.prefix = record.name;
            this.returnList.push(record.name);
            this._getWebprc();
        },
        _getWebprc (reset = false) {
            /* eslint-disable */
            this.$mask();
            if (reset) {
                 this.webrpcParams.params.prefix = '';
            }
            this.webrpcParams.params.bucketName = this.$store.state.userId;
            axios.defaults.headers.common.authorization = `Bearer ${getStorage('token')}`;
            axios.post('/minio/webrpc', this.webrpcParams).then((data)=>{
                let list = data.data.result.objects;
                list = this.filterType(list);
                list.forEach((item, index)=>{
                    if (list[index].size === 0) {
                        list[index].selectAble = false;
                    }
                });
                let table = this.$refs.table;
                this.$nextTick(()=>{
                    this.$unmask();
                    table && table.loadData(list || []);
                })
            });
        },
        encodeTime (time) {
            return encodeDate(time);
        },
        filterType(list){
            return list.filter(item=>/.csv$/.test(item.name) || item.size === 0);
        },
        getTableData () {
            return this.$refs.table;
        },
        getJsonValue () {
            return this.$refs.table.getSelections();
        },
        setJsonValue () {
        },
        MbTranslate(value){
            let val = value / BYTE / BYTE;
            return val.toFixed(5);
        }
    }
};
</script>

<style lang="less">
.select-file__table {
    width: 600px;
    height: 430px;
    padding: 0 15px;
    // .sfv-table{
    //     height: 400px;
        
    // }
    .return{
        margin: 0 0 10px;
    }
}

</style>

